<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>包装对象</title>
</head>
<body>
<script type="text/javascript">
  //三种数据类型 数值、字符串、布尔值
  var num = 100;
  var str='abcd';
  var bl = true;
  console.log(typeof num, typeof str, typeof bl);

  //实例化内置构造函数
  var num_obj = new Number(100);
  var str_obj = new String("abcd");
  var bl_obj = new Boolean(true);
  console.log(typeof num_obj, typeof str_obj, typeof bl_obj);

  console.log(num_obj === num); //false
  console.log(str_obj === str); //false
  console.log(bl_obj === bl); //false

  console.log(num_obj == num);  //true
  console.log(str_obj == str);  //true
  console.log(bl_obj == bl);  //true

  //数据类型判断 typeof
  var a=100,b='200',c=false,d=undefined,e=null,f={name:"lisi",age:30},g=function (){return 1;},h=[1,2,3];
  console.log("number = " + typeof a);  //number
  console.log("string = " + typeof b);  //string
  console.log("boolean = " + typeof c);  //boolean
  console.log("undefined = " + typeof d);  //undefined
  //还需要进一步判断
  console.log("null = " + typeof e);  //object    属于null类型
  console.log("object = " + typeof f);  //object
  console.log("function = " + typeof g);  //function  属于object类型
  console.log("array = " + typeof h);  //object    属于object类型

  //区分null  对象  数组
  //instanceof 判断一个值，是否是某一个够着函数的实例对象
  var e = null, f={"age":30}, g=function() {}, h=[3,1,2];
  console.log("null instanceof = " + (e instanceof Object)); //false
  console.log("object instanceof = " + (f instanceof Object));  //true
  console.log("function instanceof = " + (g instanceof Object));  //true
  console.log("array instanceof = " + (h instanceof Object));  //true
  console.log(g instanceof Function); //true
  console.log(g instanceof Array);  //false
  console.log(h instanceof Object);
  console.log(h instanceof Array);  //true
  console.log(h instanceof Function);  //false

  //封装一个函数，精确判断数据类型 object->array->Object
  //主要用来区分重合的function\array\null都为object
  function get_type(n) {
    if(typeof n === "object") {
        if(!(n instanceof Object)) {
            return "null"
        }
        if(n instanceof Function) {
            return "function"
        }
        if(n instanceof Array) {
            return "array"
        }else {
            return "object"
        }
    } else {
        return typeof n;
    }
  }

  console.log("e(null) = " + get_type(e));
  console.log("f(object) = " + get_type(f));
  console.log("g(function) = " + get_type(g));
  console.log("h(array) = " + get_type(h));
</script>
</body>
</html>